
**List experiment
***
use "$data/4_individual_ano_vars.dta", clear

keep if wave == 3

** Payments under the table
** Double list experiment

** Some inital cleaning and sample restricting
keep 		if group_op != "" // keep only those that were asked the question
drop 		if employer==1 // drop employers if any
destring 	group_op, force replace
drop		if status == 3

replace		opinions_2 = .r if opinions_2 == -99
replace 	opinions_2 = .d if opinions_2 == -88

** Question q0901: Treatment: Group A (q0901_a); Control: Group B (q0901_b)
** Question q0902: Treatment: Group B (q0902_b): Control: Group A (q0902_a)

*Rename variables accordingly
ren q0901_a treatment_1
ren q0901_b control_1
ren q0902_b treatment_2
ren q0902_a control_2

*Generate 2 groups (group1 - treatment in question 1 / group2 - treatment in question 2)
gen group1 = 1 if group_op==1 
replace group1=0 if group1==.

gen group2 = 1 if group_op==2
replace group2=0 if group2==.

replace group_op = 0 if group_op == 2 
**Respondents in Group 1 respond to control_1 and treatment_2
**Respondents in Group 0 respond to treatment_1 and control_2
egen count_a = rowtotal( treatment_1 control_1)
egen count_b = rowtotal( treatment_2 control_2)


****
* Doule difference in means estimator
****

* EST(P)=1/2[(x ̄_5A-x ̄_4B )+(Y ̄_5B-Y ̄_4A)]= 1/2[(x ̄_5A-Y ̄_4A )+(Y ̄_5B-X ̄_4B)],

**Nnonkey: Items of non-key items in both groups
	bysort id: gen id_ind3=_n
	by id: egen firm_size1=count(id_indiv)
	gen w = 1/q1401
	gen w_alt = 1/firm_size1
kict ls count_a count_b , nnonkey(4 4) condition(group_op) estimator(linear) duallist vce(cl id)
estimate store all

ta opinions_2 [aw=w]

*GammaA: Control group mean of question 1
*GammaB: Control group mean of question 2
*Delta: item count estimate of sensitive question (difference between 2 questions means) // mean of difference between treatment and control of Question 1 & difference between treatment and control of Question 2

**Do it by hand after Droitcour et al. 1991
** est_droitcour = 0.5 * ((mean(treatment_1) - mean(control_1)) + (mean(treatment_2) - mean(control_2)))

egen X_t=mean(treatment_1)
egen X_c=mean(control_1)
egen Y_t=mean(treatment_2)
egen Y_c=mean(control_2)

gen list_a_est = X_t - X_c
gen list_b_est = Y_t - Y_c


gen dual_list=0.5*((X_t-X_c)+(Y_t-Y_c)) // does not produce completly the same outcomes as the kict command
gen dual_list2=0.5*((X_t-Y_c)+(Y_t-X_c))
sum dual_list dual_list2

*** Repeat by PAP-PME assignment

kict ls count_a count_b if assignment==0 , nnonkey(4 4) condition(group_op) estimator(linear) duallist vce(cl id)
estimate store control

kict ls count_a count_b if assignment==1, nnonkey(4 4) condition(group_op) estimator(linear) duallist vce(cl id)
estimate store  assignment

esttab all assignment control using "$results\01_tables\Table_7_informal_side_payments.tex", ///
    keep(Delta:_cons) ///
    mtitle("All" "PAP-PME Treatment" "PAP-PME Control") ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    nonumbers se replace
	
	
*** Regression
estimates restore assignment
matrix b_treat = e(b)
matrix V_treat = e(V)
local est_treat = b_treat[1,1]
local se_treat = sqrt(V_treat[1,1])

estimates restore control  
matrix b_control = e(b)
matrix V_control = e(V)
local est_control = b_control[1,1]
local se_control = sqrt(V_control[1,1])

* Calculate difference and standard error
local diff_kict = `est_treat' - `est_control'
local se_diff = sqrt(`se_treat'^2 + `se_control'^2)  // Assuming independence
local t_stat = `diff_kict' / `se_diff'

* Calculate p-value (two-tailed)
local df = e(N) - 2  // Approximate degrees of freedom
local p_kict = 2*ttail(`df', abs(`t_stat'))

di ""
di "KICT Estimates Comparison:"
di "Treatment estimate: " %9.3f `est_treat' " (SE: " %9.3f `se_treat' ")"
di "Control estimate: " %9.3f `est_control' " (SE: " %9.3f `se_control' ")"
di "Difference: " %9.3f `diff_kict' " (SE: " %9.3f `se_diff' ")"
di "T-statistic: " %9.3f `t_stat'
di "P-value: " %5.3f `p_kict'

